﻿using full_leaf_framework.Effect;
using full_leaf_framework.Interact;
using full_leaf_framework.Visual;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace particles;

public class Game1 : Game {
    private GraphicsDeviceManager _graphics;
    private SpriteBatch _spriteBatch;

    public ParticleController controller;
    public InputManager input;

    public Camera camera;

    private const int SCREEN_WIDTH = 800;
    private const int SCREEN_HEIGHT = 600;

    public Game1() {
        _graphics = new GraphicsDeviceManager(this)
        {
            PreferredBackBufferWidth = SCREEN_WIDTH,
            PreferredBackBufferHeight = SCREEN_HEIGHT
        };
        _graphics.ApplyChanges();
        Content.RootDirectory = "Content";
        IsMouseVisible = true;
    }

    protected override void Initialize() {
        base.Initialize();
        input = new InputManager();
        input.InsertTrackingKeys(new Keys[3] { Keys.Up, Keys.Down, Keys.Space });
    }

    protected override void LoadContent() {
        _spriteBatch = new SpriteBatch(GraphicsDevice);
        camera = new Camera(_spriteBatch, new Rectangle(0, 0, 800, 600), new Vector2(0, 0), new Vector2(800, 600));
        controller = new ParticleController(@"Data\Particles\particles.json", Content);
    }

    protected override void Update(GameTime gameTime) {
        base.Update(gameTime);
        if (input.GetTrackingKey(Keys.Up).fired) {
            controller.Burst("burstUp", new Vector2(0, 0));
        }
        if (input.GetTrackingKey(Keys.Down).fired) {
            controller.Burst("burstDown", new Vector2(0, 0));
        }
        if (input.GetTrackingKey(Keys.Space).fired) {
            Vector2 mousePos = camera.ReturnPointerPos(input.GetTrackingMouse().pos.ToVector2());
            float angleTowardsMouse = RotateParticle.VelocityToAngle(mousePos - new Vector2(0, -150));
            controller.Burst("burstTowardsMouse", new Vector2(0, -150), angleRange : new float[2] { angleTowardsMouse - 0.15f, angleTowardsMouse + 0.15f });
        }
        input.Update(gameTime);
        controller.Update(gameTime);
        controller.Draw(camera);
    }

    protected override void Draw(GameTime gameTime) {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        _spriteBatch.Begin();
        camera.Draw();
        _spriteBatch.End();
        base.Draw(gameTime);
    }
}
